(function(){
    let zhecaishi=''
    let zhecaishi1=''
    $.fn.caching=function(){

      let shanping=  JSON.parse(localStorage.getItem('shanping')||"[]")
      ke()
        //显示所有加入购物车商品
      function ke(){
          let number=12;

          if(shanping==""){
              $('.Shopping-libiao').children('ul:eq(1)').css('opacity','0')
              $('.Shopping-libiao').children('.Shopping-zongjia').css('opacity','0')
        
          }else{
            var biaoti=""
            shanping.map(function(el,index){

                if (shanping[index].title.length>number){
                    biaoti=shanping[index].title.substr(0,number)+'...'
                }
                $('.Shopping-zongjia').append(`
                    <ol id="${shanping[index].id}">
                        <li><input type='checkbox' class="input_che" checked></li>
                        <li><img style="width:60px;height:60px;float: left;margin-top: 20px;" src="${shanping[index].picture}" alt=""></li>
                        <li><span>${biaoti}</span></li>
                        <li><button class="jian">-</button>
                            <input class="Shopping-jiake" type="text" value="${shanping[index].quantity}">
                            <button class="jia">+</button>
                        </li>
                        <li>${(shanping[index].unit).toFixed(2)}</li>
                        <li>${(shanping[index].unit*shanping[index].quantity).toFixed(2)}</li>
                        <li class="shanchu">删除</li>
                    </ol>
                `)
            })
          }
      }

        $('.Shopping-zongjia').children().children('li').children('.jian').on('click',function (){
            let jian=Number($(this).siblings('input').val())
            if (jian<=1){

            }else {
                $(this).siblings('input').val(jian-1)
                //商品标签
                let id=Number($(this).parent().parent().attr('id'))
                //商品数量
                let numberaa=Number($(this).siblings('input').val())
                //商品单价
                let danjia=Number($(this).parent().siblings().eq(3).text())
                //商品总价
                let lachu=Number($(this).siblings('input').val())*danjia
                // 赋值商品总价
                $(this).parent().siblings().eq(4).text(lachu.toFixed(2))

                let zuoyong=0
                let zhendehi=0
                shanping.map(function(el,index){
                    if (shanping[index].id==id){
                        console.log(index)
                        shan=({
                            picture:shanping[index].picture,
                            quantity:numberaa,
                            title:shanping[index].title,
                            total:lachu.toFixed(2),
                            unit:shanping[index].unit,
                            guige:shanping[index].guige,
                            id:shanping[index].id,
                            commentid:shanping[index].commentid
                        })
                        if (shanping[index].id=shan.id){
                            shanping[index]=shan
                        }
                    }
                    zuoyong+=Number(shanping[index].quantity)
                    zhendehi+=Number(shanping[index].quantity)*shanping[index].unit
                })
                zhecaishi=zuoyong
                $('.Shopping-jixugouwu').children('ul').children('li:eq(1)').text(`已选： ${zuoyong}件商品`)
                zhecaishi1=zhendehi.toFixed(2)
                $('.Shopping-jixugouwu').children('ul').children('li:eq(2)').text(`应付总额:  ${zhendehi.toFixed(2)}`)
                localStorage.setItem('shanping',JSON.stringify(shanping))
            }
        })


        $('.Shopping-zongjia').children().children('li').children('.jia').on('click',function (){
            let jia=Number($(this).siblings('input').val())
            if (jia<1){

            }else {
                $(this).siblings('input').val(jia+1)
                //商品标签
                let id=Number($(this).parent().parent().attr('id'))
                //商品数量
                let numberaa=Number($(this).siblings('input').val())
                //商品单价
                let danjia=Number($(this).parent().siblings().eq(3).text())
                //商品总价
                let lachu=Number($(this).siblings('input').val())*danjia
                // 赋值商品总价
                $(this).parent().siblings().eq(4).text(lachu.toFixed(2))

                let zuoyong=0
                let zhendehi=0
                var indexid=0
                shanping.map(function(el,index){
                    if (shanping[index].id==id){
                        shan=({
                            picture:shanping[index].picture,
                            quantity:numberaa,
                            title:shanping[index].title,
                            total:lachu.toFixed(2),
                            unit:shanping[index].unit,
                            guige:shanping[index].guige,
                            id:shanping[index].id,
                            commentid:shanping[index].commentid
                        })
                        indexid=index+1
                        if (shanping[index].id=shan.id){
                            shanping[index]=shan
                        }
                    }
                    zuoyong+=Number(shanping[index].quantity)
                    zhendehi+=Number(shanping[index].quantity)*shanping[index].unit
                })

                zhecaishi=zuoyong
                $('.Shopping-jixugouwu').children('ul').children('li:eq(1)').text(`已选： ${zuoyong}件商品`)
                zhecaishi1=zhendehi.toFixed(2)
                $('.Shopping-jixugouwu').children('ul').children('li:eq(2)').text(`应付总额:  ${zhendehi.toFixed(2)}`)
                localStorage.setItem('shanping',JSON.stringify(shanping))
            }
        })

    let zhen= $('.Shopping-zongjia').children().children('li:eq(5)').text()
    $('.Shopping-zongjia').children().children('li:eq(5)').text(Number(zhen).toFixed(2))
    }
    // 刚进入时已选价格和应付总额
    $.fn.zuiho=function(){
        let shanping=  JSON.parse(localStorage.getItem('shanping')||"[]")
        let zuizhong=0
        let zonge=0;
        shanping.map(function(el,index){
            zuizhong+=Number(shanping[index].quantity)
            zonge+=Number(shanping[index].unit)*Number(shanping[index].quantity)
        })
        $('.Shopping-jixugouwu').children('ul').children('li:eq(1)').text(`已选：  ${zuizhong}件商品`)
        $('.Shopping-jixugouwu').children('ul').children('li:eq(2)').text(`应付总额: ${zonge.toFixed(2)}`)
    }
    //全选、取消全选、反选
    $.fn.quanxuan=function(){
        let keke=$('.Shopping-jixugouwu').children('ul').children('li:eq(1)').text()
        let xiao=$('.Shopping-jixugouwu').children('ul').children('li:eq(2)').text()
        //全选和取消全选
        $('.Shopping-libiao').children('ul:eq(1)').children('li:eq(0)').children('input').on('click',function(){
            if($(this).prop('checked')==true){
                $('.Shopping-jixugouwu').children('ul').children('li:eq(1)').text(`${keke}`)
                $('.Shopping-jixugouwu').children('ul').children('li:eq(2)').text(`${xiao}`)
                if(zhecaishi!=''&&zhecaishi1!=''){
                    $('.Shopping-jixugouwu').children('ul').children('li:eq(1)').text(`已选： ${zhecaishi}件商品`)
                    $('.Shopping-jixugouwu').children('ul').children('li:eq(2)').text(`应付总额:  ${zhecaishi1}`)
                }
                $('.Shopping-zongjia').children('ol').children('li').children('input').prop('checked',true)
            }else{
                $('.Shopping-jixugouwu').children('ul').children('li:eq(1)').text(`已选： 0件商品`)
                $('.Shopping-jixugouwu').children('ul').children('li:eq(2)').text(`应付总额: 0`)

                $('.Shopping-zongjia').children('ol').children('li').children('input').prop('checked',false)
            }
        })

        $('.Shopping-zongjia').children('ol').children('li').children('.input_che').on('click',function (){
            if ($(this).prop('checked')==true){

            }else {
                $('.Shopping-libiao').children('ul:eq(1)').children('li:eq(0)').children('input').prop('checked',false)
            }
        })
        //子选框全部选中,父选框选中;子选框不全选中,父选框不选中
        var isallcheck=true;
        let guburusi=JSON.parse(localStorage.getItem('shanping')||"[]")
        let guburuer=JSON.parse(localStorage.getItem('shanping')||"[]")

        $('.Shopping-zongjia').children('ol').children('li').children('.input_che').on('click',function (){

                if ($(this).prop('checked')){
                    var arrayn=Number($(this).parent().parent().attr('id'))
                    var indexx=new Array();
                    guburuer.map(function (el,index){
                        if (el.id!=arrayn){
                        }else {
                            indexx.push(el)
                        }
                    })
                    let shuli=0
                    let jinge=0
                    indexx.map(function (el,index){
                        shuli+=el.quantity
                        jinge+=(el.unit*el.quantity)
                    })
                    $('.Shopping-jixugouwu').children('ul').children('li:eq(1)').text(`已选： ${Number(shuli)}件商品`)
                    $('.Shopping-jixugouwu').children('ul').children('li:eq(2)').text(`应付总额:  ${Number(jinge).toFixed(2)}`)

                }
            $('.Shopping-zongjia').children('ol').children('li').children('.input_che').each(function(i,o){
                if(!$(o).prop("checked")){
                    isallcheck=false;
                    return;
                }

            });
            if(isallcheck){
                $('.Shopping-libiao').children('ul:eq(1)').children('li:eq(0)').children('input').prop("checked",isallcheck);
            }else{
                var arrayn=Number($(this).parent().parent().attr('id'))
                var indexx=new Array();
                var jilu=0
                guburusi.map(function (el,index){
                    if (el.id!=arrayn){
                        indexx.push(el)
                    }else {
                        jilu=index
                    }
                })
                let shuli=0
                let jinge=0
                indexx.map(function (el,index){
                    shuli+=el.quantity
                    jinge+=(el.unit*el.quantity)
                })
                $('.Shopping-jixugouwu').children('ul').children('li:eq(1)').text(`已选： ${Number(shuli)}件商品`)
                $('.Shopping-jixugouwu').children('ul').children('li:eq(2)').text(`应付总额:  ${Number(jinge).toFixed(2)}`)
                guburusi.splice(jilu,1)
                $(this).parent().parent().attr('data-checked',false)
                $('.Shopping-libiao').children('ul:eq(1)').children('li:eq(0)').children('input').prop("checked",isallcheck);
            }
            isallcheck=true;

        })



        
        // 删除本地缓存
        $('.Shopping-zongjia').children('ol').children('.shanchu').on('click',function(){
            let numer=Number($(this).parent().attr('id'))
            var gao=new Array();
            let shanpingao=  JSON.parse(localStorage.getItem('shanping')||"[]")
            shanpingao.map(function (el,index){
                if (shanpingao[index].id!=numer){
                    gao.push(shanpingao[index])
                }
            })
            if(confirm('您确定要删除嘛?')){
                localStorage.setItem('shanping',JSON.stringify(gao))
                $(this).parent().remove()
                let zhecaish3=0
                let zhecaishi4=0
                gao.map(function (el,index){
                    zhecaish3+=el.quantity;
                    zhecaishi4+=el.unit*el.quantity
                })
                console.log(zhecaish3)
                console.log(zhecaishi4)

                $('.Shopping-jixugouwu').children('ul').children('li:eq(1)').text(`已选： ${Number(zhecaish3)}件商品`)
                $('.Shopping-jixugouwu').children('ul').children('li:eq(2)').text(`应付总额:  ${Number(zhecaishi4).toFixed(2)}`)
            }
            
        })
        ke()
        function ke(){
            let shanping=  JSON.parse(localStorage.getItem('shanping')||"[]")
            if(shanping==''){
                $('.Shopping-zongjia').css('display','none')
                $('.Shopping-libiao').children('ul:eq(1)').css('display','none')
            }else{
                $('.Shopping-zongjia').css('display','block')
                $('.Shopping-libiao').children('ul:eq(1)').css('display','block')
            }
        }
       

        $('.yang').css('marginTop','80px')
    }



})()

    $(window).load(function(){
        $('.max').caching()
        $('.max').zuiho()
        $('.max').quanxuan()
    })
